package com.nic.view.widget;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nic.view.R;
import com.nic.view.adapter.SpecialMultAdapter;
import com.nic.view.model.SpecialMultBean;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 多个多选列表
 * Created by Administrator on 2019/4/11.
 */

public class SpecialMultPopWindow extends CommonPopupWindow implements View.OnClickListener, SpecialMultAdapter.OnClickItemListener {
    private TextView tv_shadow;
    private Button bt_cancel;
    private Button bt_confirm;
    private Activity act;
    private ListView listView;
    private SpecialMultAdapter specialMultAdapter;
    private Map<Integer, String> itemIdsMap = new HashMap<>();

    public SpecialMultPopWindow(Context c) {
        super(c, R.layout.pop_mult_special, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        act = (Activity) context;
        tv_shadow = (TextView) contentView.findViewById(R.id.tv_shadow);
        tv_shadow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPopupWindow().dismiss();
            }
        });
        bt_cancel = (Button) contentView.findViewById(R.id.cancel);
        bt_cancel.setOnClickListener(this);
        bt_confirm = (Button) contentView.findViewById(R.id.confirm);
        bt_confirm.setOnClickListener(this);
        listView = (ListView) contentView.findViewById(R.id.listView);
        specialMultAdapter = new SpecialMultAdapter();
        specialMultAdapter.setOnClickItemListener(this);
        listView.setAdapter(specialMultAdapter);
    }

    public void setList(List<SpecialMultBean> list) {
        try {
            specialMultAdapter.setList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void add(SpecialMultBean bean) {
        try {
            specialMultAdapter.add(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void showAsDropDown(View view) {
        if (!getPopupWindow().isShowing() && !act.isFinishing()) {

            getPopupWindow().showAsDropDown(view);
        }
    }

    public boolean isShow() {
        return getPopupWindow().isShowing();
    }

    public void showAtLocation(View view) {
        if (!getPopupWindow().isShowing() && !act.isFinishing()) {
            getPopupWindow().showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }

    public void show() {
        if (!getPopupWindow().isShowing() && !act.isFinishing()) {
            getPopupWindow().showAtLocation(act.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        }
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.cancel) {
            getPopupWindow().dismiss();

        } else if (i == R.id.confirm) {
            getPopupWindow().dismiss();
            if (onMultiListener != null) {
                onMultiListener.onSpecialMultiItem(itemIdsMap);
            }
        }


    }


    private OnMultiListener onMultiListener;

    public void setOnMultiListener(OnMultiListener onMultisListener) {
        this.onMultiListener = onMultisListener;
    }

    @Override
    public void checkedItem(Map<Integer, Map<String, String>> checkedList) {
        if (checkedList.isEmpty()) {
            itemIdsMap.clear();
            return;
        }
        //取出map中下标不变，Map<Integer, String>转id，id，
        for (Map.Entry<Integer, Map<String, String>> bean : checkedList.entrySet()) {
            Map<String, String> item = bean.getValue();
            if (item.size() > 0) {
                StringBuffer sb1 = new StringBuffer();
                for (Map.Entry<String, String> entry : item.entrySet()) {
                    sb1.append(entry.getValue() + ",");
                }
                itemIdsMap.put(bean.getKey(), sb1.toString().substring(0, sb1.length() - 1));
            } else {
                if (itemIdsMap.containsKey(bean.getKey())) {
                    itemIdsMap.remove(bean.getKey());
                }
            }
        }
    }

    public interface OnMultiListener {
        void onSpecialMultiItem(Map<Integer, String> map);
    }

    public void OnDismissListener(PopupWindow.OnDismissListener listener) {
        getPopupWindow().setOnDismissListener(listener);
    }

    class MapKeyComparator implements Comparator<Integer> {

        @Override
        public int compare(Integer int1, Integer int2) {

            return int1.compareTo(int2);
        }
    }

    /**
     * 使用 Map按key进行排序
     *
     * @param map
     * @return
     */
    public Map<Integer, Map<Integer, String>> sortMapByKey(Map<Integer, Map<Integer, String>> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<Integer, Map<Integer, String>> sortMap = new TreeMap<Integer, Map<Integer, String>>(
                new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }
}
